package org.eclipse.emf.validation.examples.general.constraints;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.validation.model.ConstraintSeverity;
import org.eclipse.emf.validation.model.EvaluationMode;
import org.eclipse.emf.validation.service.AbstractConstraintDescriptor;
import org.eclipse.uml2.uml.Constraint;

public class OCLUMLConstraintDescriptor extends AbstractConstraintDescriptor {
	private final Constraint constraint;
	private final String id;
	private final String name;
	private final String namespace;
	private final int code;
	
	public OCLUMLConstraintDescriptor(String namespace, Constraint constraint, int code) {
		this.constraint = constraint;
		
		String name = constraint.getName();
		if (name == null) {
			name = Long.toHexString(System.identityHashCode(constraint));
		}
		
		id = namespace + '.' + name;
		this.name = name;
		this.namespace = namespace;
		this.code = code;
	}
	
	final Constraint getConstraint() {
		return constraint;
	}
	
	public String getBody() {
		return constraint.getSpecification().stringValue();
	}

	public String getDescription() {
		return getBody();
	}

	public EvaluationMode getEvaluationMode() {
		return EvaluationMode.BATCH;
	}

	public String getId() {
		return id;
	}

	public String getMessagePattern() {
		return "Constraint " + getName() + " violated on {0}";
	}

	public String getName() {
		return name;
	}

	public String getPluginId() {
		return namespace;
	}

	public ConstraintSeverity getSeverity() {
		return ConstraintSeverity.WARNING;
	}

	public int getStatusCode() {
		return code;
	}

	public boolean targetsEvent(Notification notification) {
		return false;
	}

	public boolean targetsTypeOf(EObject eObject) {
		return true;
	}

}
